﻿package com.edward.service;

import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.edward.PagerModel;
import com.edward.model.Cargo;

public class CargoManager extends HibernateDaoSupport {
	
	private JdbcTemplate jdbcTemplate;
	
	

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	public void addCargo(Cargo cargo) {

		getHibernateTemplate().save(cargo);
	}

	public void updateCargo(Cargo cargo) {

		getHibernateTemplate().update(cargo);
	}

	public void deleteCargo(int id) {

		getHibernateTemplate().delete(getCargo(id));
	}

	public Cargo getCargo(int id) {

		Cargo c = (Cargo) getHibernateTemplate().get(Cargo.class, id);
		return c;
	}

	@SuppressWarnings("unchecked")
	public List<Cargo> getCodes(String query,String location) {
		List<Cargo> list = getHibernateTemplate().find(
				"from Cargo c where c.code like '" + query + "%' and c.location = '" + location + "'" );
		return list;
	}
//用于采购单查询不重复的数据列表
	@SuppressWarnings("unchecked")
	public List getCodes(String query) {
		String sql = "select  distinct c.code from cargo c where c.code like '%" + query + "%'" ;
		List list = jdbcTemplate.queryForList(sql);
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public Cargo getCode(String query) {
		List<Cargo> list = getHibernateTemplate().find(
				"from Cargo c where c.code like '" + query + "'");
		Cargo c = list.get(0);
		return c;
	}

	@SuppressWarnings("unchecked")
	public List<Cargo> getNames(String query,String location) {
		List<Cargo> list = getHibernateTemplate().find(
				"from Cargo c where c.name like '%" + query + "%' and c.location = '" + location + "'" );
		return list;
	}

	@SuppressWarnings("unchecked")
	public List getNames(String query) {
		String sql = "select  distinct c.name from cargo c where c.name like '%" + query + "%'" ;
		List list = jdbcTemplate.queryForList(sql);
		return list;
	}
	@SuppressWarnings("unchecked")
	public Cargo getName(String query) {
		List<Cargo> list = getHibernateTemplate().find(
				"from Cargo c where c.name like '" + query + "'");
		if(list.size()>0){
		Cargo c = list.get(0);
			return c;
		}else{
			return null;
		}
		
	}

	@SuppressWarnings("unchecked")
	public PagerModel pagerCargo(int start, int limit , boolean oversea) {
		List <Cargo>list;
		if(oversea){
			list = getHibernateTemplate().find("from Cargo c where c.location in ('E','F','G','X','Y')" );
		}else{
			list = getHibernateTemplate().find("from Cargo c where c.location in ('A','B','C','X','Y')" );
		}
		int total = list.size();
		int toIndex = start + limit;
		if (toIndex > total) {
			toIndex = total;
		}
		List<Cargo> datas = list.subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;

	}

	@SuppressWarnings("unchecked")
	public PagerModel pagerCargo(int start, int limit, Cargo cargo ,boolean oversea) {
		int count = 0;

		String sql = "from Cargo c where ";
		if (!cargo.getCode().equals("")) {
			sql += "c.code = '" + cargo.getCode() + "'";
			count++;
		}
		if (!cargo.getName().equals("")) {
			if (count > 0) {
				sql += " and ";
			}
			count++;
			sql += "c.name = '" + cargo.getName() + "'";
		}
		if(!cargo.getLocation().equals("")&&!cargo.getLocation().equals("请选择")){
			if (count > 0) {
				sql += " and ";
			}
			count++;
			sql += "c.location = '" + cargo.getLocation() + "'";
		}else{
		
			if(oversea){
				sql += "and c.location in ('E','F','G','X','Y')";
			}else{
				sql += "and c.location in ('A','B','C','X','Y')";
			}
		}

		int total = getHibernateTemplate().find(sql).size();
		int toIndex = start + limit;
		if (toIndex > total) {
			toIndex = total;
		}
		List<Cargo> datas = getHibernateTemplate().find(sql).subList(start,
				toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;

	}

	//检查在同仓库中有无相同的货物
	@SuppressWarnings("unchecked")
	public boolean check(Cargo c) {
		List<Cargo> list1 = getHibernateTemplate().find(
				"from Cargo c where c.code = '" + c.getCode()
						+ "' and c.location = '" + c.getLocation()
						+ "' and c.id != " + c.getId());
		List<Cargo> list2 = getHibernateTemplate().find(
				"from Cargo c where c.name = '" + c.getName()
						+ "' and c.location = '" + c.getLocation() 
						+ "' and c.id != " + c.getId());
		if (list1.size() > 0 || list2.size() > 0) {
			return true;
		} else {
			return false;
		}

	}

	//检查有没同名称或者编号的货物
	@SuppressWarnings("unchecked")
	public boolean updateCheck(Cargo c) {
		List<Cargo> list1 = getHibernateTemplate().find(
				"from Cargo c where c.code = '" + c.getCode()
						+ "' and c.location = '" + c.getLocation() + "'");
		List<Cargo> list2 = getHibernateTemplate().find(
				"from Cargo c where c.name = '" + c.getName()
						+ "' and c.location = '" + c.getLocation() + "'");

		if (list1.size() > 1 || list2.size() > 1) {
			return true;
		} else {
			return false;
		}

	}
	
	//检查该仓库有没同编号的货物
	@SuppressWarnings("unchecked")
	public Cargo checkCargo(String location , String code) {
		List<Cargo> list = getHibernateTemplate().find(
				"from Cargo c where c.code = '" + code
						+ "' and c.location = '" + location + "'");

		if (list.size() > 0 ) {
			return list.get(0);
		} else {
			return null;
		}

	}
	
	

}
